package longcaisuyun.longcai.com.yiliubabalongcaisuyun;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.module.pull.PullToRefreshBase;
import com.module.pull.PullToRefreshListView;
import com.zcx.helper.http.AsyCallBack;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import longcaisuyun.longcai.com.net.PostMonth;
import longcaisuyun.longcai.com.suyunbean.MonthBean;

//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                            O\ = /O
//                        ____/`---'\____
//                      .   ' \\| |// `.
//                       / \\||| : |||// \
//                     / _||||| -:- |||||- \
//                       | | \\\ - /// | |
//                     | \_| ''\---/'' | |
//                      \ .-\__ `-` ___/-. /
//                   ___`. .' /--.--\ `. . __
//                ."" '< `.___\_<|>_/___.' >'"".
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |
//                 \ \ `-. \_ __\ /__ _/ .-` / /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//
//         .............................................
//         佛祖保佑             永无BUG
//         佛曰:
//                  写字楼里写字间，写字间里程序员；
//                  程序人员写程序，又拿程序换酒钱。
//                  酒醒只在网上坐，酒醉还来网下眠；
//                  酒醉酒醒日复日，网上网下年复年。
//                  但愿老死电脑间，不愿鞠躬老板前；
//                  奔驰宝马贵者趣，公交自行程序员。
//                  别人笑我忒疯癫，我笑自己命太贱；
//                  不见满街漂亮妹，哪个归得程序员？
/************************************************************/
//        pulllist.onPullDownRefreshComplete();

//        pulllist.onPullUpRefreshComplete();

/************************************************************/

public class MonthActivity extends Activity implements View.OnClickListener {

    PullToRefreshListView pulllist;
    LinearLayout lin_fh;
    TextView t_history;
    String code = "";
    TextView   textView34;
    String startpage = "1";
    String nextpage = "";
    String ispage = "";
    List<MonthBean> list = new ArrayList<MonthBean>();
    MonthAdapter m = new MonthAdapter(MonthActivity.this,list);
    TextView   textView;
    String   month="";
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_month);
        View view = getWindow().getDecorView();
        // 适配工具将视图重新排布
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) view);

        code = getIntent().getStringExtra("code");
        month=code.substring(4, 6);
        ViewInit();
        Log.e("month",month);
        HttpPost(code, startpage);
    }

    private void ViewInit() {


        textView= (TextView) findViewById(R.id.textView33);
        textView.setText(month+"月份订单收入");
        pulllist = (PullToRefreshListView) findViewById(R.id.pulllist);
        textView34= (TextView) findViewById(R.id.textView34);
//        t_history = (TextView) findViewById(R.id.t_history);
        lin_fh = (LinearLayout) findViewById(R.id.fanhui_lin);
        lin_fh.setOnClickListener(this);
//        t_history.setOnClickListener(this);
        pulllist.setScrollLoadEnabled(false);

        pulllist.setPullLoadEnabled(true);


        pulllist.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {

            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                HttpPost(code, startpage);
            }


            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

            }
        });
        ListView listView = pulllist.getRefreshableView();
        listView.setDivider(null);
        View view = View.inflate(MonthActivity.this, R.layout.list_footer, null);
        MyApplication.ScaleScreenHelper.loadView((ViewGroup) view);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ispage.equals("1")) {
                    HttpPost(code, nextpage);
                } else {
                    Toast.makeText(MonthActivity.this, "已经加载完毕", Toast.LENGTH_SHORT).show();
                }
            }
        });
        listView.addFooterView(view);
        listView.setAdapter(m);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fanhui_lin:
                this.finish();
                break;
            case R.id.t_history:
                startActivity(new Intent(MonthActivity.this, HistoryActivity.class));
                MonthActivity.this.finish();
                break;
        }
    }

    public void HttpPost(String code, String page) {
        MyApplication.myviewutil.startProgressDialog(MonthActivity.this);
        new PostMonth(MyApplication.myPreferences.readUid(), code, "1", new AsyCallBack<PostMonth.Info>() {
            @Override
            public void onSuccess(String toast, int type, PostMonth.Info info) throws Exception {
                super.onSuccess(toast, type, info);
                if (info.message.equals("1")) {
                    ispage = info.ispage;
                    nextpage = info.nextpage;
                    textView34.setText(info.account+"元");
                    addlist(info.jsonArray);
                    m.notifyDataSetChanged();
                } else {
                    Toast.makeText(MonthActivity.this, "获取数据失败，请稍后重新尝试", Toast.LENGTH_SHORT).show();
                    MyApplication.myviewutil.stopProgressDialog();
                    pulllist.onPullDownRefreshComplete();
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                MyApplication.myviewutil.stopProgressDialog();
                pulllist.onPullDownRefreshComplete();
                Toast.makeText(MonthActivity.this, "获取数据失败，请检查您的网络设置后重新尝试", Toast.LENGTH_SHORT).show();
                super.onFail(toast, type);
            }
        }).execute(MonthActivity.this);
    }

    public void addlist(JSONArray jsonArray) {
        list.clear();
        for (int j = 0; j < jsonArray.length(); j++) {
            MonthBean monthBean = new MonthBean();
            try {
                JSONObject jsonObject = (JSONObject) jsonArray.get(j);
                monthBean.setPosttime(jsonObject.getString("posttime"));
                monthBean.setMoney(jsonObject.getString("money"));
                list.add(monthBean);
                Log.e("duoshao",""+list.size());
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        MyApplication.myviewutil.stopProgressDialog();
        pulllist.onPullDownRefreshComplete();
    }
}
